﻿Imports System.Data.Sql
Imports System.Data.SqlClient
Imports DTO
Public Class PhieuNhapDAO

    Dim dp As New DataProvider
    Function LoadPN() As DataTable
        Dim phieunhap As New DataTable
        phieunhap = dp.ExecuteQuery("LoadPN", Nothing)
        Return phieunhap
    End Function

    Function LayTheoID(ByVal PN As Integer) As DTO.PhieuNhapDTO
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaPN", PN)
        Dim dt = cn.ExecuteQuery("LayPNTheoID", para)
        Dim phieunhap As New PhieuNhapDTO()
        If dt.Rows.Count <> 0 Then
            phieunhap.MaPN = Integer.Parse(dt.Rows(0).Item("MaPN").ToString())
            phieunhap.NgayNhap = DateTime.Parse(dt.Rows(0).Item("Ngaynhap").ToString())
            phieunhap.MaKho = Integer.Parse(dt.Rows(0).Item("MaKho").ToString())
            phieunhap.MaSP = Integer.Parse(dt.Rows(0).Item("MaSP").ToString())
            phieunhap.SoLuong = Integer.Parse(dt.Rows(0).Item("Soluong").ToString())
        End If
        Return phieunhap
    End Function

    Function LayTatCa() As Collections.IList
        Dim cn As New DataProvider()
        Dim array As New ArrayList
        Dim dt = cn.ExecuteQuery("layTatCaPN")
        If dt.Rows.Count <> 0 Then
            For Each dr As DataRow In dt.Rows
                Dim phieunhap As New PhieuNhapDTO()
                phieunhap.MaPN = Integer.Parse(dr.Item("MaPN").ToString())
                phieunhap.MaSP = Integer.Parse(dr.Item("MaSP").ToString())
                phieunhap.MaKho = Integer.Parse(dr.Item("MaKho").ToString())
                phieunhap.NgayNhap = DateTime.Parse(dr.Item("Ngaynhap").ToString())
                phieunhap.SoLuong = Integer.Parse(dr.Item("SoLuong").ToString())
                array.Add(phieunhap)
            Next
        End If
        Return array
    End Function

    Function LayTatCaTT() As DataTable
        Dim cn As New DataProvider()
        Return cn.ExecuteQuery("layTatCaPN")
    End Function

    Sub Them(ByVal pn As PhieuNhapDTO)
        Dim cn As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(3) {}
        para(0) = New SqlParameter("@MaSP", pn.MaSP)
        para(1) = New SqlParameter("@ngaynhap", pn.NgayNhap)
        para(2) = New SqlParameter("@makho", pn.MaKho)
        para(3) = New SqlParameter("@soluong", pn.SoLuong)
        cn.ExecuteNonQuery("InsertPN", para)
    End Sub

    Sub CapNhap(ByVal pn As PhieuNhapDTO)
        Dim cn As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(3) {}
        para(0) = New SqlParameter("@MaSP", pn.MaSP)
        para(1) = New SqlParameter("@ngaynhap", pn.NgayNhap)
        para(2) = New SqlParameter("@makho", pn.MaKho)
        para(3) = New SqlParameter("@soluong", pn.SoLuong)
        cn.ExecuteNonQuery("CapNhapPN", para)
    End Sub

    Sub Xoa(ByVal id As Integer)
        Dim cn As New DataProvider()
        Dim parar = New SqlParameter("@mapn", id)
        cn.ExecuteNonQuery("DeletePN", parar)
    End Sub

End Class
